package org.example.word.parser;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.example.word.AbstractWordDocumentParser;
import org.example.word.pojo.TestProgressFrom;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TestProgressFormParser extends AbstractWordDocumentParser<TestProgressFrom> {
    
    @Override
    protected Map<String, String> getFieldMapping() {
        return new HashMap<>(); // 这个表单使用特殊处理逻辑
    }
    
    @Override
    protected Class<TestProgressFrom> getResultType() {
        return TestProgressFrom.class;
    }
    
    @Override
    protected void processSpecialFields(XWPFDocument document, TestProgressFrom result) {
        XWPFTable table = document.getTables().getFirst();
        Map<String, String> versionTests = new HashMap<>();
        Map<String, String> hardwareTests = new HashMap<>();
        Map<String, String> softwareTests = new HashMap<>();
        
        boolean isConclusionRow = false;
        
        for (XWPFTableRow row : table.getRows()) {
            List<XWPFTableCell> cells = row.getTableCells();
            String firstCellText = cells.get(0).getText().trim();
            
            if (firstCellText.contains("电鸿适配进度测试确认表")) {
                result.setVendor(firstCellText.split("：")[1]);
            } else if ("设备信息".equals(firstCellText)) {
                continue;
            } else if ("汇总（结论）".equals(firstCellText)) {
                result.setVersionAdaptationTestResults(cells.get(1).getText().trim());
                result.setHardwareAdaptationTestResults(cells.get(2).getText().trim());
                result.setSoftwareAdaptationTestResults(cells.get(3).getText().trim());
                isConclusionRow = true;
            } else if (firstCellText.contains("物资品类")) {
                continue;
            } else if (!isConclusionRow) {
                if (result.getDeviceInfo() == null && !firstCellText.isEmpty()) {
                    result.setDeviceInfo(firstCellText);
                }
                
                if (cells.size() > 2 && !cells.get(1).getText().trim().isEmpty()) {
                    versionTests.put(cells.get(1).getText().trim(), cells.get(2).getText().trim());
                }
                if (cells.size() > 4 && !cells.get(3).getText().trim().isEmpty()) {
                    hardwareTests.put(cells.get(3).getText().trim(), cells.get(4).getText().trim());
                }
                if (cells.size() > 6 && !cells.get(5).getText().trim().isEmpty()) {
                    softwareTests.put(cells.get(5).getText().trim(), cells.get(6).getText().trim());
                }
            }
        }
        
        // 映射版本适配测试结果
        result.setVersionConsistency(versionTests.get("版本一致性"));
        result.setVersionCompliance(versionTests.get("版本合规性"));
        result.setVersionIntegrity(versionTests.get("版本完整性"));
        
        // 映射硬件整体适配测试结果
        result.setResourceEfficiency(hardwareTests.get("资源高效"));
        result.setConnectionPortable(hardwareTests.get("连接便携"));
        result.setApplyPower(hardwareTests.get("应用赋能"));
        result.setSecurityFeatures(hardwareTests.get("安全特性"));
        
        // 映射软件整体适配测试结果
        result.setCloudConnection(softwareTests.get("云侧连接"));
        result.setSystemApplicationUpgrade(softwareTests.get("系统应用升级"));
        result.setSystemFirmwareUpgrade(softwareTests.get("系统固件升级"));
        result.setSystemAlgorithmUpgrade(softwareTests.get("系统算法升级"));
        result.setLogManagement(softwareTests.get("日志管理"));
        result.setIntelligentOperations(softwareTests.get("智能运维"));
        result.setPnp(softwareTests.get("即插即用"));
        result.setTerminalAuthentication(softwareTests.get("终端身份认证"));
    }
} 